<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>图算法 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="一、图需要的宏定义123#define MAX_LIST 15#define INFINITE 1000000#define MAX_LINE 1024 二、图的表结构12345678910111213141516171819202122232425262728293031323334typedef struct&amp;#123;	int id;    unsigned char name[10];&#x2F;&#x2F;">
<meta property="og:type" content="article">
<meta property="og:title" content="图算法">
<meta property="og:url" content="https://goofyer.gitee.io/notes-on-computer-expertise/2021/12/13/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/1801%20%E7%AC%AC%E5%85%AB%E7%AB%A0%20%E5%9B%BE%20%E5%9B%BE%E7%AE%97%E6%B3%95/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="一、图需要的宏定义123#define MAX_LIST 15#define INFINITE 1000000#define MAX_LINE 1024 二、图的表结构12345678910111213141516171819202122232425262728293031323334typedef struct&amp;#123;	int id;    unsigned char name[10];&#x2F;&#x2F;">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-12-13T06:46:54.000Z">
<meta property="article:modified_time" content="2021-12-18T13:55:00.345Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/notes-on-computer-expertise/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/notes-on-computer-expertise/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/notes-on-computer-expertise/css/style.css">

  
    
<link rel="stylesheet" href="/notes-on-computer-expertise/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/notes-on-computer-expertise/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/notes-on-computer-expertise/">Home</a>
        
          <a class="main-nav-link" href="/notes-on-computer-expertise/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/notes-on-computer-expertise/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://goofyer.gitee.io/notes-on-computer-expertise"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-数据结构/1801 第八章 图 图算法" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/notes-on-computer-expertise/2021/12/13/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/1801%20%E7%AC%AC%E5%85%AB%E7%AB%A0%20%E5%9B%BE%20%E5%9B%BE%E7%AE%97%E6%B3%95/" class="article-date">
  <time class="dt-published" datetime="2021-12-13T06:46:54.000Z" itemprop="datePublished">2021-12-13</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">数据结构基础知识</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      图算法
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="一、图需要的宏定义"><a href="#一、图需要的宏定义" class="headerlink" title="一、图需要的宏定义"></a>一、图需要的宏定义</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">#define MAX_LIST 15</span><br><span class="line">#define INFINITE 1000000</span><br><span class="line">#define MAX_LINE 1024</span><br></pre></td></tr></table></figure>
<h1 id="二、图的表结构"><a href="#二、图的表结构" class="headerlink" title="二、图的表结构"></a>二、图的表结构</h1><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="keyword">int</span> id;</span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> name[<span class="number">10</span>];<span class="comment">//景点名称</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> intro[<span class="number">10</span>];</span><br><span class="line">&#125;Tourist;</span><br><span class="line">Tourist tourist[MAX_LIST];</span><br><span class="line"><span class="keyword">int</span> num=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="keyword">int</span> list[MAX_LIST];</span><br><span class="line">	<span class="keyword">int</span> size;</span><br><span class="line">&#125;SeqList;</span><br><span class="line">SeqList seq;</span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">    <span class="keyword">int</span> vexs[MAX_LIST];                  <span class="comment">//顶点</span></span><br><span class="line">    <span class="keyword">int</span>        arcs[MAX_LIST][MAX_LIST];  <span class="comment">//边</span></span><br><span class="line">    <span class="keyword">int</span>        vexnum, arcnum;</span><br><span class="line">&#125;MGraph;</span><br><span class="line">MGraph g;</span><br><span class="line"><span class="comment">//辅助数组中的元素定义 </span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">int</span> distance;</span><br><span class="line">    <span class="keyword">int</span> path[MAX_LIST];</span><br><span class="line">&#125;ArrayNode;</span><br><span class="line"></span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">char</span> *msg[MAX_LIST] = &#123; (<span class="keyword">unsigned</span> <span class="keyword">char</span>*)<span class="string">&quot;体育馆1\0\0&quot;</span>,(<span class="keyword">unsigned</span> <span class="keyword">char</span>*)<span class="string">&quot;体育馆2\0\0&quot;</span>,(<span class="keyword">unsigned</span> <span class="keyword">char</span>*)<span class="string">&quot;体育馆3\0\0&quot;</span>,(<span class="keyword">unsigned</span> <span class="keyword">char</span>*)<span class="string">&quot;体育馆4\0\0&quot;</span>,(<span class="keyword">unsigned</span> <span class="keyword">char</span>*)<span class="string">&quot;体育馆5\0\0&quot;</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> pathOrder = <span class="number">0</span>;  <span class="comment">//记录到当前找到的路径个数 </span></span><br><span class="line"><span class="keyword">int</span> startPoint, endPoint;</span><br><span class="line"><span class="keyword">int</span> path[<span class="number">100</span>], visited[<span class="number">100</span>] = &#123; <span class="number">0</span> &#125;;</span><br><span class="line"><span class="keyword">int</span> pathNumber = <span class="number">0</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MaxVertexNum 100</span></span><br><span class="line"><span class="comment">//O(n^2)</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> </span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="keyword">char</span> vex[MaxVertexNum];</span><br><span class="line">	<span class="keyword">int</span> edge[MaxVertexNum][MaxVertexNum];</span><br><span class="line">	<span class="keyword">int</span> vexnum, arcnum;</span><br><span class="line">&#125;MGragh;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">///</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">G 无向图  存储空间：O(|V|+2|E|)</span></span><br><span class="line"><span class="comment">  有向图  存储空间：O(|V|+|E|)</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">arcNode</span>;</span></span><br><span class="line"><span class="comment">//定点表</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="keyword">char</span> data;</span><br><span class="line">	arcNode* first;</span><br><span class="line">&#125;VNode,AdjList[MaxVertexNum];</span><br><span class="line"><span class="comment">//边节点</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	<span class="keyword">int</span> adjvex;</span><br><span class="line">	arcNode *next;</span><br><span class="line">	<span class="keyword">int</span> length;</span><br><span class="line">&#125;arcNode;</span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">	AdjList vetice;</span><br><span class="line">	<span class="keyword">int</span> vexnum, arcnum;</span><br><span class="line">&#125;MGragh2;</span><br></pre></td></tr></table></figure>
<h1 id="三、图的操作"><a href="#三、图的操作" class="headerlink" title="三、图的操作"></a>三、图的操作</h1><h2 id="3-1-图的初始化"><a href="#3-1-图的初始化" class="headerlink" title="3.1 图的初始化"></a>3.1 图的初始化</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">void Init()</span><br><span class="line">&#123;</span><br><span class="line">    for (int i = 0; i &lt; MAX_LIST; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        for (int j = 0; j &lt; MAX_LIST; j++)</span><br><span class="line">        &#123;</span><br><span class="line">            g.arcs[i][j] = INFINITE;</span><br><span class="line">        &#125;</span><br><span class="line">        g.vexs[i] = -1;</span><br><span class="line">    &#125;</span><br><span class="line">    g.arcnum = 0;</span><br><span class="line">    g.vexnum = 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br></pre></td><td class="code"><pre><span class="line">void InsertList(int i)</span><br><span class="line">&#123;</span><br><span class="line">	if (seq.size &lt; MAX_LIST)</span><br><span class="line">	&#123;</span><br><span class="line">		seq.list[seq.size]=i;</span><br><span class="line">		seq.size++;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">int GetListnum()</span><br><span class="line">&#123;</span><br><span class="line">	return seq.size;</span><br><span class="line">&#125;</span><br><span class="line">void DeleteList(int i)</span><br><span class="line">&#123;</span><br><span class="line">	if (i &gt;= 0 &amp;&amp; i &lt; MAX_LIST)</span><br><span class="line">	&#123;</span><br><span class="line">		for (int j = i + 1; j &lt; seq.size; j++)</span><br><span class="line">		&#123;</span><br><span class="line">			seq.list[j] = seq.list[j + 1];</span><br><span class="line">		&#125;</span><br><span class="line">		seq.size--;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line">int GetList(int i)</span><br><span class="line">&#123;</span><br><span class="line">	return seq.list[i];</span><br><span class="line">&#125;</span><br><span class="line">//添加景点信息</span><br><span class="line">void InsertTour(int id,char name[10],char intro[10])</span><br><span class="line">&#123;</span><br><span class="line">	tourist[num].id = id;</span><br><span class="line">	strcpy(tourist[num].name , name);</span><br><span class="line">	strcpy(tourist[num].intro , intro);</span><br><span class="line">	num++;</span><br><span class="line">&#125;</span><br><span class="line">//添加顶点</span><br><span class="line">void insertVex(int id, char name[10], char intro[10])</span><br><span class="line">&#123;</span><br><span class="line">    InsertTour(id, name, intro);</span><br><span class="line">    g.vexs[g.vexnum] = id;</span><br><span class="line">    g.vexnum++;</span><br><span class="line">&#125;</span><br><span class="line">//添加边</span><br><span class="line">void insertarc(int a,int b,int length)</span><br><span class="line">&#123;</span><br><span class="line">    g.arcs[a][b] = length;</span><br><span class="line">    g.arcs[b][a] = length;</span><br><span class="line">    g.arcnum++;</span><br><span class="line">&#125;</span><br><span class="line">//更新路径长度</span><br><span class="line">void UpdataLoadLength(int a, int b, int length)</span><br><span class="line">&#123;</span><br><span class="line">    g.arcs[a][b] = length;</span><br><span class="line">    g.arcs[b][a] = length;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">void Reads()</span><br><span class="line">&#123;</span><br><span class="line">	int d=setlocale(0, &quot;chs&quot;);</span><br><span class="line">	//下面是读数据，将读到的数据存到数组a[10]中，并且打印到控制台上</span><br><span class="line">	FILE* fpRead = fopen(&quot;1.txt&quot;, &quot;r&quot;);</span><br><span class="line">	if (fpRead == NULL)</span><br><span class="line">	&#123;</span><br><span class="line">		return;</span><br><span class="line">	&#125;</span><br><span class="line">	int n;</span><br><span class="line">	int error=fscanf(fpRead, &quot;%d&quot;, &amp;n);</span><br><span class="line">	Init();</span><br><span class="line">	for (int i = 0; i &lt; n; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		int id;</span><br><span class="line">        unsigned char name[10];</span><br><span class="line">        unsigned char intro[10];</span><br><span class="line">		int error = fscanf(fpRead, &quot;%d %s %s&quot;, &amp;id, &amp;name, &amp;intro);</span><br><span class="line">        strcpy( name,msg[i] );</span><br><span class="line">		InsertList(id);</span><br><span class="line">		insertVex(id, name, intro);</span><br><span class="line">	&#125;</span><br><span class="line">	for (int i = 0; i &lt; 6; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		int a, b, length;</span><br><span class="line">		int mm=fscanf(fpRead, &quot;%d %d %d&quot;, &amp;a, &amp;b, &amp;length);</span><br><span class="line">		insertarc(a-1, b-1, length);</span><br><span class="line">	&#125;	</span><br><span class="line">	int cc=fclose(fpRead);</span><br><span class="line">&#125;</span><br><span class="line">//迪杰斯特拉算法---解决最短路径问题</span><br><span class="line">void Dijkstra(int from, int to)</span><br><span class="line">&#123;</span><br><span class="line">    int i, j, index = -1;</span><br><span class="line">    int n = 1;//记录已经求出的两个点之间的最短距离的个数 </span><br><span class="line">    ArrayNode shortestPath[MAX_LIST];</span><br><span class="line">    int flag[MAX_LIST] = &#123; 0 &#125;;//标记，为1表示到这个顶点的最短距离已求出 </span><br><span class="line"></span><br><span class="line">    //1.求from到各个顶点的直接距离，即初始化shortestPath数组 </span><br><span class="line">    for (i = 0; i &lt; g.vexnum; i++) &#123;</span><br><span class="line">        if (from == i) &#123;</span><br><span class="line">            shortestPath[i].distance = 0;</span><br><span class="line">            shortestPath[i].path[0] = i;</span><br><span class="line">            flag[from] = 1;</span><br><span class="line">        &#125;</span><br><span class="line">        else if (g.arcs[from][i] &gt; 0) &#123;</span><br><span class="line">            shortestPath[i].path[0] = from;</span><br><span class="line">            shortestPath[i].path[1] = i;</span><br><span class="line">            shortestPath[i].distance = g.arcs[from][i];</span><br><span class="line">        &#125;</span><br><span class="line">        else</span><br><span class="line">            shortestPath[i].distance = INFINITE;</span><br><span class="line">    &#125;</span><br><span class="line">    //2.每次求一个最短路径 </span><br><span class="line">    while (n &lt; g.vexnum) &#123;</span><br><span class="line">        //选择shortestPath中距离最小的，求出from到这个顶点的最短路径 </span><br><span class="line">        index = -1;</span><br><span class="line">        for (i = 0; i &lt; g.vexnum; i++) &#123;</span><br><span class="line">            if (i == from)</span><br><span class="line">                continue;</span><br><span class="line">            if (flag[i] == 0 &amp;&amp; index == -1 &amp;&amp; shortestPath[i].distance != INFINITE)</span><br><span class="line">                index = i;</span><br><span class="line">            if (flag[i] == 0 &amp;&amp; index != -1 &amp;&amp; shortestPath[i].distance &lt; shortestPath[index].distance)</span><br><span class="line">                index = i;</span><br><span class="line">        &#125;</span><br><span class="line">        flag[index] = 1;</span><br><span class="line">        //修改到各个顶点的最短路径 </span><br><span class="line">        for (i = 0; i &lt; g.vexnum; i++) &#123;</span><br><span class="line">            if (i == from)</span><br><span class="line">                continue;</span><br><span class="line">            if (g.arcs[index][i] &gt; 0 &amp;&amp; g.arcs[index][i] + shortestPath[index].distance &lt; shortestPath[i].distance) &#123;</span><br><span class="line">                shortestPath[i].distance = g.arcs[index][i] + shortestPath[index].distance;</span><br><span class="line">                //修改路径 </span><br><span class="line">                j = 0;</span><br><span class="line">                while (1) &#123;</span><br><span class="line">                    shortestPath[i].path[j] = shortestPath[index].path[j];</span><br><span class="line">                    if (shortestPath[index].path[j] == index)</span><br><span class="line">                        break;</span><br><span class="line">                    j++;</span><br><span class="line">                &#125;</span><br><span class="line">                shortestPath[i].path[j + 1] = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        n++;</span><br><span class="line">    &#125;</span><br><span class="line">    //输出from到to的最短路径及长度 </span><br><span class="line">    if (shortestPath[to].distance == INFINITE) &#123;</span><br><span class="line">        printf(&quot;%s到%s没有路径\n&quot;, tourist[from].name, tourist[to].name);</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">    printf(&quot;%s到%s的最短路径长度是：%d\n&quot;, tourist[from].name, tourist[to].name, shortestPath[to].distance);</span><br><span class="line">    printf(&quot;经过的地址是：  &quot;);</span><br><span class="line">    i = 0;</span><br><span class="line">    while (1) &#123;</span><br><span class="line">        printf(&quot;-%s&quot;, tourist[shortestPath[to].path[i]].name);</span><br><span class="line">        if (shortestPath[to].path[i] == to)</span><br><span class="line">            break;</span><br><span class="line">        i++;</span><br><span class="line">    &#125;</span><br><span class="line">    printf(&quot;\n&quot;);</span><br><span class="line">&#125;</span><br><span class="line">//解决所有路径问题</span><br><span class="line">int first(int n)</span><br><span class="line">&#123;</span><br><span class="line">    for (int i = 0; i &lt; g.vexnum; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        if (g.arcs[n][i] != INFINITE&amp;&amp;n!=i)</span><br><span class="line">            return i;</span><br><span class="line">    &#125;</span><br><span class="line">    printf(&quot;起点为故障点，请输入一个不为故障点的起点\n&quot;);</span><br><span class="line">    return 1000;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int sumCount(int n)</span><br><span class="line">&#123;</span><br><span class="line">    int count = 0;</span><br><span class="line">    for (int i = 0; i &lt; g.vexnum; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        if (g.arcs[n][i] == INFINITE)</span><br><span class="line">            count++;</span><br><span class="line">    &#125;</span><br><span class="line">    return count;</span><br><span class="line">&#125;</span><br><span class="line">int next(int n, int now)</span><br><span class="line">&#123;</span><br><span class="line">    for (int i = now + 1; i &lt; g.vexnum; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        if (g.arcs[n][i] != INFINITE)</span><br><span class="line">            return i;</span><br><span class="line">    &#125;</span><br><span class="line">    return 1000;  //当找不到节点n的下一个节点，返回1000</span><br><span class="line">&#125;</span><br><span class="line">void DFS(int nowPoint, int count)</span><br><span class="line">&#123;</span><br><span class="line">    visited[nowPoint] = 1;</span><br><span class="line">    path[count++] = nowPoint;</span><br><span class="line">    if (nowPoint == endPoint &amp;&amp; (count) &gt;= 1)</span><br><span class="line">    &#123;</span><br><span class="line">        pathNumber++;</span><br><span class="line">        printf(&quot;这两个地址间第%d条路径为: &quot;, pathNumber);</span><br><span class="line">        for (int i = 0; i &lt; (count)-1; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            printf(&quot;%d--&gt;&quot;, path[i] + 1);</span><br><span class="line">        &#125;</span><br><span class="line">        printf(&quot;%d\n&quot;, path[(count)-1] + 1);</span><br><span class="line">    &#125;</span><br><span class="line">    else</span><br><span class="line">    &#123;</span><br><span class="line">        int k;</span><br><span class="line">        for (k = first(nowPoint); k &lt; g.vexnum; k = next(nowPoint, k))</span><br><span class="line">        &#123;</span><br><span class="line">            if (visited[k] == 0)</span><br><span class="line">                DFS(k, count);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    visited[nowPoint] = 0;  //算法关键，回溯，将访问过状态变为未访问状态 </span><br><span class="line">    (count)--;</span><br><span class="line">    return;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://goofyer.gitee.io/notes-on-computer-expertise/2021/12/13/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/1801%20%E7%AC%AC%E5%85%AB%E7%AB%A0%20%E5%9B%BE%20%E5%9B%BE%E7%AE%97%E6%B3%95/" data-id="cl403sxs8003if8vugw9c009c" data-title="图算法" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/notes-on-computer-expertise/2021/12/14/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/1205-%E7%AC%AC%E4%BA%8C%E7%AB%A0%E7%BA%BF%E6%80%A7%E8%A1%A8-%E4%B9%A0%E9%A2%98%E6%80%BB%E7%BB%93/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          1205 第二章线性表-习题总结
        
      </div>
    </a>
  
  
    <a href="/notes-on-computer-expertise/2021/12/12/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/1201%20%E7%AC%AC%E4%BA%8C%E7%AB%A0%E7%BA%BF%E6%80%A7%E8%A1%A8-%E5%AE%9A%E4%B9%89/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">链表</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/C-%E5%B7%A5%E5%85%B7/">C#工具</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/C-%E5%B7%A5%E5%85%B7/">C++工具</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/Mysql/">Mysql</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/blender/">blender</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/linux/">linux</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/manim/">manim</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/spring/">spring</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/vba%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C/">vba基础操作</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E5%88%9B%E9%80%A0%E6%A8%A1%E5%BC%8F/">创造模式</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">操作系统基础知识</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%80%E5%8F%91/">操作系统开发</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E5%AD%A6/">数学</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">数据结构基础知识</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%A8%A1%E6%9D%BF/">模板</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%8B%B1%E8%AF%AD/">英语</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%99%9A%E5%B9%BB4/">虚幻4</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/">计算机组成原理</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/">计算机网络</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/notes-on-computer-expertise/tags/C/" rel="tag">C++</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/notes-on-computer-expertise/tags/C/" style="font-size: 10px;">C++</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/03/">March 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2021/12/">December 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/31/vue/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6%E9%AA%8C%E8%AF%81%E7%A0%81%E6%97%A0%E6%B3%95%E6%98%BE%E7%A4%BA/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/26/%E6%95%B0%E6%8D%AE%E5%BA%93/navicat%E5%AE%89%E8%A3%85/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/25/%E8%99%9A%E5%B9%BB4%E5%BC%95%E6%93%8E%E5%BC%80%E5%8F%91/%E8%99%9A%E5%B9%BB%E5%9B%9B%20mod%E5%88%B6%E4%BD%9C/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/23/python/python%E6%89%B9%E9%87%8F%E7%94%9F%E6%88%90%E6%95%B0%E6%8D%AE/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/23/vba%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C%E7%AC%94%E8%AE%B0/EXCEL%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2022 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/notes-on-computer-expertise/" class="mobile-nav-link">Home</a>
  
    <a href="/notes-on-computer-expertise/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/notes-on-computer-expertise/js/jquery-3.4.1.min.js"></script>



  
<script src="/notes-on-computer-expertise/fancybox/jquery.fancybox.min.js"></script>




<script src="/notes-on-computer-expertise/js/script.js"></script>





  </div>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</body>
</html>